import React from 'react'
import { Table } from 'antd'
import BaseTable from '@comp/Table/index'

export default class SourceTable extends BaseTable {
    constructor(props: any) {
        super(props)
        this.state = {
            columns: [],
            pageConfig: {
                showSizeChanger: true,
                defaultCurrent: 1,
                defaultPageSize: 5,
                total: this.props.total,
                showTotal: total => `共 ${total} 条`,
                pageSizeOptions: ["5", "10", "20", "50"],
                onChange: (page: number, pageSize?: number) => {
                    this.props.pageChange && this.props.pageChange(page, pageSize)
                }
            }
        }
    }
    public componentDidMount() {
        this.setState({ columns: this.props.columns })
    }
    public componentDidUpdate(preProps: any) {
        if (this.props.total !== preProps.total) {
            this.setState({ pageConfig: Object.assign(this.state.pageConfig, { total: this.props.total }) })
        }
    }
    public renderTable() {
        const { dataSource, loading, expandable, scroll, selectRow } = this.props
        const { columns, pageConfig } = this.state
        const rowSelection: any = {
            onChange: (selectedRowKeys: any) => {
                selectRow && selectRow(selectedRowKeys)
            }
        }
        if (selectRow !== undefined) {
            return (
                <div className="source_table">
                    <Table
                        rowKey="Id"
                        bordered={true}
                        columns={columns}
                        dataSource={dataSource}
                        loading={loading}
                        expandable={expandable}
                        pagination={pageConfig}
                        scroll={scroll}
                        rowSelection={rowSelection}>
                    </Table>
                </div>
            )
        } else {
            return (
                <div className="source_table">
                    <Table
                        rowKey="Id"
                        bordered={true}
                        columns={columns}
                        dataSource={dataSource}
                        loading={loading}
                        expandable={expandable}
                        pagination={pageConfig}
                        scroll={scroll}
                    >
                    </Table>
                </div>
            )
        }
    }
}